# -*- coding:utf-8 -*-

import os
from openpyxl import load_workbook, workbook


class ExcelImpldic:
    def __init__(self, path=None):
        # self.count=0
        if path == None:
            self.resultpath = os.getcwd() + "\\inputdata\\resnum.xlsx"
        else:
            self.resultpath = path

        self.book = load_workbook(self.resultpath)

    def writeStatusSheet(self, dist=None, type=None):

        book = self.book
        if type == "single":

            work_sheet = book.get_sheet_by_name("single")
        elif type == "batch":

            work_sheet = book.get_sheet_by_name("batch")
        else:
            work_sheet = book.get_sheet_by_name("num")

        # 清空
        # print self.count
        # if self.count==0:
        self.cleanSheet(work_sheet)

        # maxrow = eri.get_rows_count(work_sheet)
        i = 1
        for key in dist:
            work_sheet.cell(row=i, column=1).set_explicit_value(value=str(key))
            work_sheet.cell(row=i, column=2).set_explicit_value(value=str(dist[key]))
            i = i + 1

        self.book.save(self.resultpath)

    def cleanSheet(self, workSheet):
        for r in range(1, workSheet.max_row + 1):
            for c in range(1, workSheet.max_column + 1):
                workSheet.cell(row=r, column=c).set_explicit_value(value=None, data_type="n")
        # self.count=1
        self.book.save(self.resultpath)

    def gettestdic(self, type):

        dist = {}

        work_sheet = self.book.get_sheet_by_name(type)

        for j in range(1, work_sheet.max_row + 1):
            dist[work_sheet.cell(row=j, column=1).value] = work_sheet.cell(row=j, column=2).value

        return dist


class ExcelReadImpl:
    def get_rows_count(self, worksheet):
        for i in range(1, 65535):
            val = worksheet.cell(row=i, column=1).value
            if val == None:
                return i

    def get_col_count(self, worksheet):
        for i in range(1, 65535):
            val = worksheet.cell(row=1, column=i).value
            if val == None:
                return i
